/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.dummydevice.monitors;

import java.util.Collections;
import java.util.List;

import net.zarubsys.robotplayground.core.monitors.IMonitor;
import net.zarubsys.robotplayground.core.monitors.IMonitorListener;
import net.zarubsys.robotplayground.core.session.ModuleSessionContainer;
import net.zarubsys.robotplayground.core.session.RunningSession;
import net.zarubsys.robotplayground.core.session.Session;
import net.zarubsys.robotplayground.core.session.SessionsHelper;
import net.zarubsys.robotplayground.dummydevice.monitors.DummyPositionMonitor;
import net.zarubsys.robotplayground.gui.dummydevice.plugin.DummyDeviceGUIPlugin;
import net.zarubsys.robotplayground.gui.session.providers.IRunListener;
import net.zarubsys.robotplayground.gui.session.providers.ISystemObjectGUIProvider;
import net.zarubsys.robotplayground.gui.session.providers.SystemObjectGUIProviderClass;
import net.zarubsys.robotplayground.gui.session.providers.SystemObjectGUIProvidersHelper;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * DummyPositionMonitorGUIProvider
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyPositionMonitorGUIProvider implements ISystemObjectGUIProvider, IRunListener {
	
	private static final Logger LOG = Logger.getLogger(DummyPositionMonitorGUIProvider.class);
	
	private static int viewCounter = 0;

	@Override
	public Image getImage(String id) {
		return DummyDeviceGUIPlugin.getDefault().getImage(DummyDeviceGUIPlugin.DUMMY_POSITION);
	}

	@Override
	public String getDescription(String id) {
		return "Position";
	}

	@Override
	public SystemObjectGUIProviderClass getProviderClass() {
		return SystemObjectGUIProviderClass.MONITOR;
	}

	@Override
	public int addObjectToSession(Session session, String id) {
		session.getMonitors().add(id);
		return session.getMonitors().size() - 1;
	}

	@Override
	public boolean reconfigure(Session session, int index) {
		return false;
	}

	@Override
	public boolean isConfigurable(String id) {
		return false;
	}

	@Override
	public boolean remove(Session session, int index) {
		session.getMonitors().remove(index);
		return true;
	}

	@Override
	public String getDescription(Session session, int index) {
		return getDescription(session.getMonitors().get(index));
	}

	@Override
	public String validate(Session session, int index) {
		IMonitor monitor = SessionsHelper.getInstance().getMonitor(session.getMonitors().get(index));
 		List<String> requiredModules = monitor.getRequiredModules();
		O:for (String requiredModuleId : requiredModules) {
			for (ModuleSessionContainer moduleCont : session.getModules()) {
				if (moduleCont.getModuleId().equals(requiredModuleId)) continue O;
			}
			ISystemObjectGUIProvider requiredModuleProvider = SystemObjectGUIProvidersHelper.getInstance().getProvider(SystemObjectGUIProviderClass.MODULE, requiredModuleId);
			if (requiredModuleProvider == null) {
				return "Missing required module for id '" + requiredModuleId + "'. There is even no provider for it.";
			}
			return "Missing required module '" + requiredModuleProvider.getDescription(requiredModuleId) + "'.";
		}
		return null;
	}

	@Override
	public synchronized void notifyRuns(RunningSession runningSession, int indexInClass) {
		IMonitor monitor = runningSession.getMonitors().get(indexInClass);
		Assert.isTrue(monitor instanceof DummyPositionMonitor, "Expected " + DummyPositionMonitor.class.getName() + ", got " + monitor.getClass().getName());
		DummyPositionMonitor positionMonitor = (DummyPositionMonitor) monitor;
		
		try {
			DummyPositionMapView view = (DummyPositionMapView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(
					DummyPositionMapView.ID, "" + viewCounter++, IWorkbenchPage.VIEW_ACTIVATE);
			view.initMonitor(positionMonitor);
			positionMonitor.registerMonitorListeners(Collections.singletonList((IMonitorListener) view));
		} catch (PartInitException e) {
			LOG.error("Error while openning new view!", e);
		}
	}
	
}

